package com.sh169.service.auth.impl;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sh169.beans.UserHolder;
import com.sh169.pojos.Role;
import com.sh169.pojos.User;
import com.sh169.pojos.Vendor;
import com.sh169.service.BaseServiceImpl;
import com.sh169.service.auth.AuthHomeService;

@IocBean(name="authHomeService")
public class AuthHomeServiceImpl extends BaseServiceImpl implements AuthHomeService {
	@Inject("refer:dao")
	private Dao dao;
	public Dao getDao() {
		return dao;
	}
	public void setDao(Dao dao) {
		this.dao = dao;
	}
	public UserHolder loadUserByUserName(String username) {
		User user = dao.fetch(User.class, username);
		if(user==null) return null;
		dao.fetchLinks(user, "roles");
		UserHolder holder = new UserHolder(); 
		holder.setUsername(user.getUsername());
		holder.setPassword(user.getPassword());
		holder.setUserState(user.getState());
		List<String> roles = new ArrayList<String>();
		if(user.getRoles()==null || user.getRoles().size() ==0)
			holder.setRoles(roles);
		else {
			for(Role r : user.getRoles()){
				roles.add(r.getId());
			}
			holder.setRoles(roles);
		}
		if(user.getType() == User.TYPE_VENDOR){
			Vendor vendor = dao.fetch(Vendor.class,Cnd.where("userid","=",user.getId()));
			holder.setVendor(vendor);
		}
		return holder;
	}
	public String regist(User user) {
		
		
		return null;
	}

}
